using System;
using System.Collections.Generic;
using System.Text;

namespace Caece.Libreria.Protocolo
{
    public enum DNSProtocolCommand
    {
        NewFrontEnd,
        ReponseNewFrontEnd,
        DameLista,
        ResponseDameLista,
        DameMayores,
        ResponseDameMayores,
        DameServidorFronEnd,
        ResponseDameServidorFronEnd


    }

    public class DNSProtocol
    {
        public static byte[] Encode(DNSProtocolCommand command, string param)
        {
            List<byte> result = new List<byte>();
            result.AddRange(BitConverter.GetBytes((int)command));
            result.AddRange(Encoding.UTF8.GetBytes(param));
            return result.ToArray();
        }

        public static DNSProtocolCommand Decode(byte[] data, out string param)
        {
            DNSProtocolCommand command = (DNSProtocolCommand)BitConverter.ToInt32(data, 0);

            param = Encoding.UTF8.GetString(data, 4, data.Length - 4);

            return command;
        }

        #region Encoders
        public static string EncodeReponseNewFrontEnd(int proceso)
        {
            return proceso.ToString();
        }


        public static string EncodeIpPuerto(int pPuerto, byte[] pIp)
        {
            string ret = String.Empty;

            ret = pPuerto.ToString() + "|";
            for (int i = 0; i < 4; i++)
            {
                ret = ret + pIp[i] + ".";
            }
            ret = ret.Substring(0, ret.Length - 1);
            return ret;

        }

        public static string EncodeResponseDameLista(List<clsInfo> lista)
        {
            String ret = String.Empty;

            foreach (clsInfo cNodo in lista)
            {
                String auxStr = cNodo.ProcesoId + "|" +
                        ByteArrayToStr(cNodo.Ip) + "|" + cNodo.Puerto + "|";
                ret = ret + auxStr;

            }
			if (ret.Length > 0)
				ret = ret.Substring(0, ret.Length - 1);

            return ret;
        }



        public static string EncodeResponseDameMayores(List<clsInfo> lista)
        {
            String ret = String.Empty;

            foreach (clsInfo cNodo in lista)
            {
                String auxStr = cNodo.ProcesoId + "|" +
                        ByteArrayToStr(cNodo.Ip) + "|" + cNodo.Puerto + "|";
                ret = ret + auxStr;

            }
            ret = ret.Substring(0, ret.Length - 1);

            return ret;
        }

        public static byte[] StrToByteArray(string str)
        {
            string[] ByteStrings;
            ByteStrings = str.Split(".".ToCharArray());
            byte[] ByteOut;

            ByteOut = new byte[ByteStrings.Length];

            for (int i = 0; i < ByteStrings.Length; i++)
            {
                ByteOut[i] = Convert.ToByte(ByteStrings[i]);
            }

            return ByteOut;


            //System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            //return encoding.GetBytes(str);
        }


        public static string ByteArrayToStr(byte[] param)
        {
            string salida = String.Empty;
            for (int i = 0; i < param.Length; i++)
            {
                salida = salida + param[i].ToString() + ".";
            }

            salida = salida.Substring(0, salida.Length - 1);

            return salida;

            //System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            //return encoding.GetString(param);
        }

        #endregion

        #region Decoders
        public static Int32 DecodeReponseNewFrontEnd(string param)
        {
            return Int32.Parse(param);
        }

        public static clsInfo ResponseDameServidorFronEnd(string param)
        {
            string[] Cadena = param.Split(new char[] { '|' });

            clsInfo cNodo = new clsInfo(StrToByteArray(Cadena[1]), Int16.Parse(Cadena[0]), 0);

            return cNodo;
        }



        public static List<clsInfo> DecodeResponseDameLista(string param)
        {
            List<clsInfo> lista = new List<clsInfo>();
            string[] listaaux = param.Split(new char[] { '|' });
            int i = 0;

            if (listaaux.Length > 0)
            {
                while (i < listaaux.Length)
                {
                    clsInfo nodito = new clsInfo(StrToByteArray(listaaux[i + 1].ToString()),
                        Int16.Parse(listaaux[i + 2]), Int16.Parse(listaaux[i]));

                    lista.Add(nodito);
                    i = i + 3;
                }

            }

            return lista;

        }



        public static List<clsInfo> DecodeResponseDameMayores(string param)
        {
            List<clsInfo> lista = new List<clsInfo>();
			if (param.Length > 0)
			{
				string[] listaaux = param.Split(new char[] { '|' });
				int i = 0;

				if (listaaux.Length > 0)
				{
					while (i < listaaux.Length)
					{
						clsInfo nodito = new clsInfo(StrToByteArray(listaaux[i + 1].ToString()),
							Int16.Parse(listaaux[i + 2]), Int16.Parse(listaaux[i]));

						lista.Add(nodito);
						i = i + 3;
					}
				}
			}
            return lista;

        }


        #endregion

    }

}
